package com.jet.chat.dbquery;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class ChatQueryViewUnread extends ChatQueryAbstract {

	@Override
	protected ChatQueryResult main(Connection conn, ChatQueryParams chatQueryParams) throws Exception {
		int userId = ((ChatQueryViewUnreadParams) chatQueryParams).getUserId();
		
		ChatQueryViewUnreadResult result = new ChatQueryViewUnreadResult();
		
		PreparedStatement ps = conn.prepareStatement("select m.id, m.timestamp, u.username, m.pub, m.channelname, m.content from messages as m, isread as i, users as u where m.id = i.messageid and u.id = m.senderid and i.userId = ? and isread = ?");
		ps.setInt(1, userId);
		ps.setInt(2, 0);
		ResultSet results = ps.executeQuery();
		while(results.next()) {
			ChatQueryViewUnreadResultRaw raw = new ChatQueryViewUnreadResultRaw(
				results.getInt(1),
				results.getTimestamp(2),
				results.getString(3),
				results.getInt(4),
				results.getString(5),
				results.getString(6)
			);
			result.add(raw);
		}
		results.close();
		
		return result;
	}

}
